package main

import (
	"io"
	"log"
	"math/rand"
	"net/http"
	_ "net/http/pprof"
	"os"
	"strconv"
	"time"
	_ "time"

	"cncmp_homework/model10/homework1/httpserver/metrics"
)

func main() {
	log.Println("Starting http server...")
	http.HandleFunc("/healthz", healthz)
	err := http.ListenAndServe(":50233", nil)
	if err != nil {
		log.Fatal(err)
	}

}

func healthz(w http.ResponseWriter, r *http.Request) {
	log.Printf("客户端IP地址为：%s\n", r.RemoteAddr)
	timer := metrics.NewTimer()
	defer timer.ObserveTotal()
	randInt := rand.Intn(2000)
	time.Sleep(time.Millisecond * time.Duration(randInt))
	version := os.Getenv("VERSION")
	w.Header().Add("VERSION", version)
	for k, v := range r.Header {
		w.Header().Add(k, v[0])
		log.Printf("Header:%s=%s", k, v[0])
	}
	io.WriteString(w, "success\n")
	io.WriteString(w, strconv.Itoa(randInt))
}
